package script.mainpage
{
	import laya.events.Event;
	import laya.ui.Button;
	import laya.ui.View;
	
	import model.ConstantModel;
	import model.Userdata;
	
	import script.baseView.BaseViewController;
	
	import ui.MainPagePanelUI;
	import ui.learn.LearnWordsPanelUI;
	import ui.tabview.ChooseSubjectPanelUI;
	import ui.tabview.UserCenterPanelUI;
	
	public class MainPageController extends BaseViewController
	{
		private var uiSkin:MainPagePanelUI;
		
		private var tabbtns:Array = [];
		
		private var viewArr:Array;
		private var curView:View;

		private var hasOpenView:Array;
		private var curViewIndex:int = -1;

		public function MainPageController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as MainPagePanelUI;
			
			viewArr = [ChooseSubjectPanelUI,ChooseSubjectPanelUI,UserCenterPanelUI];

			tabbtns.push(uiSkin.learntbn);
			tabbtns.push(uiSkin.quizbn);
			tabbtns.push(uiSkin.userbtn);

			hasOpenView = new Array(tabbtns.length);

			for(var i:int=0;i < tabbtns.length;i++)
			{
				tabbtns[i].on(Event.CLICK,this,onClickTabView,[i]);
				
			}
			
		}
		
		private function onClickTabView(index:int):void
		{
			
			
			if(curViewIndex == index)
				return;
			curViewIndex = index;
			
			if(curViewIndex == 0)
				Userdata.instance.curState = ConstantModel.LEARN_MODEL;
			else if(curViewIndex == 1)
				Userdata.instance.curState = ConstantModel.QUIZ_MODEL;

				
			while(uiSkin.tancontainer.numChildren > 0)
			{
				uiSkin.tancontainer.removeChildAt(0);
				
			}
			for(var i:int=0;i < tabbtns.length;i++)
			{
				(tabbtns[i] as Button).selected = i==index;
			}
			if(hasOpenView[index] != null)
			{
				uiSkin.tancontainer.addChild(hasOpenView[index]);

			}
			else if(viewArr[index])
			{
				curView = new viewArr[index]();
				uiSkin.tancontainer.addChild(curView);
				
				hasOpenView[index] = curView;
			}
			
		}
		
		
	}
}